package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequestMapping("/list")
    @RequirePermission(name = "结算单查询", expression = "consumption:list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo) {
        model.addAttribute("pageInfo", consumptionService.query(qo));
        // 门店下拉选
        model.addAttribute("businesses", businessService.listAll());
        return "consumption/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "结算单删除", expression = "consumption:delete")
    public String delete(Long id) {
        if (id != null) {
            // 通过结算单id 查出结算单,再通过流水号,查出跟结算单相关的详情
            List<ConsumptionItem> consumptionItemList =
                    consumptionItemService.queryByCno(consumptionService.get(id).getCno());
            for (ConsumptionItem consumptionItem : consumptionItemList) {
               consumptionItemService.delete(consumptionItem.getId());
            }
            consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }

    @RequestMapping("/input")
    @RequirePermission(name = "结算单编辑", expression = "consumption:saveOrUpdate")
    public String input(Long id, Model model) {
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            // 结算单 用于回显
            model.addAttribute("consumption", consumption);
            // 通过结算单号查到详情
           List<ConsumptionItem> items= consumptionItemService.queryByCno(consumption.getCno());
           // 结算单详情
            model.addAttribute("items",items);
        }
        // 结算类型
        model.addAttribute("settlementType", systemDictionaryService.queryBySn("settlement_type"));
        // 业务
        model.addAttribute("category", systemDictionaryService.queryBySn("business"));
        // 门店下拉选
        model.addAttribute("businesses", businessService.listAll());

        return "consumption/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "结算单编辑", expression = "consumption:saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() != null) {
            consumptionService.update(consumption);
        } else {
            consumptionService.insert(consumption);
        }
        // input保存的时候 带上结算单的id 回到input页面
        return "redirect:/consumption/input?id="+consumption.getId();
    }

    @RequestMapping("/save")
    @RequirePermission(name = "结算单编辑(有预约)", expression = "consumption:save")
    public String save(Long aid) {
        // 通过预约单id 创建一个结算单 并拿到结算单id
        Long cid = consumptionService.save(aid);
        // 返回input页面带上 结算单id
        return "redirect:/consumption/input?id=" + cid;
    }

    // 结算
    @RequestMapping("/consume")
    public String consume(Consumption consumption) {
        Long cid = consumptionService.consume(consumption);
        // 返回input页面带上 结算单id
        return "redirect:/consumption/input?id=" + cid;
    }

    // 审核
    @RequestMapping("/audit")
    public String audit(Consumption consumption) {
        Long cid = consumptionService.audit(consumption);
        // 返回input页面带上 结算单id
        return "redirect:/consumption/input?id=" + cid;
    }

    // 异步提交表单方式
    /*@RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "结算单编辑", expression = "consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Consumption consumption) {
        if (consumption.getId() != null) {
            consumptionService.update(consumption);
        } else {
            consumptionService.insert(consumption);
        }
        return new JsonResult(true, "添加成功");
    }*/
}
